package com.heo.client.app;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import org.apache.log4j.Logger;

import io.netty.channel.Channel;

/**
 * Created by zhangcf 本类是用来存放所有的socket连接
 */
public class ClientSocketChannelMap
{
  private static Logger log = Logger.getLogger(ClientSocketChannelMap.class);
  /**
   * 结构 线程的索引号index,SocketChannel
   */
  private static ConcurrentHashMap<Integer, Channel> socketMap = new ConcurrentHashMap<Integer, Channel>();// 线程安全对象，记录当前每个socket连接对应的通道信息

  public static void add(Integer index, Channel channel)
  {
    try
    {
      socketMap.put(index, channel);
    }
    catch (Exception e)
    {
      log.error(e.getMessage(), e);
    }
  }

  public static int getSocketSize()
  {
    return socketMap.size();
  }

  public static Channel getChannel(Integer index)
  {
    return socketMap.get(index);
  }

  public static void remove(Integer index)
  {
    try
    {
      socketMap.remove(index);
    }
    catch (Exception e)
    {
      log.error(e.getMessage(), e);
    }
  }
}
